<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_vote\service;
use cmf\controller\PluginBaseController;
use think\Db;
use think\Config;
use plugins\qbt_vote\model\VoteModel;
class VoteService extends PluginBaseController
{

    public function _initialize()
    {
        $this->config = Config::get('database');
    }   
    
    public function add($data){
        if(strtotime($data['start_time']) >= strtotime($data['end_time'])){
            $this->error('开始时间需小于结束时间！');
        }
        $VoteModle = new VoteModel();
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['create_time'] = time();
        $data['state'] = 0;
        $data['city'] = @implode(',',$data['city']);
        $data['everytime'] = intval($data['everytime']);
        $validate = $this->validate($data, 'Vote');
        
        if(true !== $validate){
            // 验证失败 输出错误信息
            $this->error($validate);
        }

        $result = $VoteModle->allowField(true)->insert($data);
        return $result;
    }
    
    public function edit($data){
        if(strtotime($data['start_time']) >= strtotime($data['end_time'])){
            $this->error('开始时间需小于结束时间！');
        }
        $VoteModle = new VoteModel();
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);

        $data['everytime'] = intval($data['everytime']);
        $data['city'] = @implode(',',$data['city']);
        $validate = $this->validate($data, 'Vote');
    
        if(true !== $validate){
            // 验证失败 输出错误信息
            $this->error($validate);
        }
    
        $result = $VoteModle->allowField(true)->isUpdate(true)->save($data);
        return $result;
    }

    public function info($id){
        $VoteModle = new VoteModel();
        $vote = $VoteModle->get($id);
        $item = Db::name('vote_item')->where('vote_id',$id)->select();
        $maxcount = Db::name('vote_item')->where('vote_id',$id)->Max('count');
        return ['vote'=>$vote,'item'=>$item,'maxcount'=>$maxcount];
    }
    
    public function vote($id){
        if(empty($id)){
            $this->error('请选择');
        }
        $ip = get_client_ip();
        $item = Db::name('vote_item')->where(['id'=>$id[0]])->find();
        
        $vote = Db::name('vote')->where(['id'=>$item['vote_id']])->find();
        
        $votecount = count($id);
        if($vote['state'] == 1){
            $this->error('投票已关闭');
        }
        
        if($vote['start_time'] > time()){
            $this->error('投票未开始');
        }
        
        if($vote['end_time'] < time()){
            $this->error('投票已结束');
        }

        if($vote['city']){
            //如果有区域限制则根据ip区域查询
            $city = $this->ip_city($ip);
            if (strpos($city,$vote['city']) ===false){
                $this->error('您所在地区不能参与投票！');
            }
        }
        
        if($votecount > $vote['everytime']){
            $this->error('最多只能选择'.$vote['everytime'].'个项目');
        }
        
        $check = Db::name('vote_log')->where(['ip'=>$ip,'vote_id'=>$vote['id']])->find();
        
        if(!empty($check)){
            $this->error('你已经投过票了');
        }
        $cookie = cookie('vote');
        $arr = json_decode($cookie,true);
        if(!empty($arr)&&in_array($vote['id'], $arr)){
            $this->error('你已经投过票了');
        }
        
        $arr = json_decode($cookie,true);
        $arr[] = $vote['id'];
        
        cookie('vote',json_encode($arr),3600*24*360);
        
        foreach ($id as $key=>$value){
            $item = Db::name('vote_item')->where(['id'=>$value])->find();
            if(!empty($item)){
                $insert['vote_id'] = $item['vote_id'];
                $insert['item_id'] = $item['id'];
                $insert['ip'] = $ip;
                $insert['create_time'] = time();
                Db::name('vote_log')->insert($insert);
                Db::name('vote_item')->where(['id'=>$item['id']])->setInc('count');
            }
        
        }
        
        $this->success('投票成功');
    }

    private function ip_city($ip){
        $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
        $ip=json_decode(file_get_contents($url));
        $data = $ip->data;
        return $data->city;
    }
}
